Beginner's Guide to Modern PC Demo Coding

Frood

Introduction

I am still convinced I was born in the right year. If I saw the likes of State of Mind, 303 or Juice I would have been put off programming for life! It is much easier to feel inspired when looking at a spinning wireframe cube and a bouncing scroll text.

This is the reason I am going to write this series of tutorials for the newbie demo coder... they have more guts than me!

What you need

Persistence and Enthusiasm. Without those two you will fail. Coders like Statix only had to learn new techniques as they came out. Now we have a lot more to learn but I will help in trying to get you there as soon and as painlessly as possible.

You will need a copy of a 32 bit compiler, Watcom, Borland C++ 4.5+ or Visual C++. I personally use Borland C++ 4.5 and code will be presented as such but it should be compatible with all.

Beginning

Stage 1. Watch some demos. I am sure most of you will have done that already so you are partly the way there, but since you are now a demo coder you have to look at demos in a new light. What's going on in each stage? How many effects are happening at once. How much 2D and 3D are used? Are some effects being used together (i.e. Plasma on a 3D model). This will give you a new perspective on demos and one that you need when you want to come up with your own ideas.

Stage 2. OpenGL or DirectX? People will argue either way on this, but using one is a must. It is very difficult to code any graphics in Windows without one of these libraries. Personally I use DirectX, and this is what I will use in this tutorial. But if you want to be able to port your demos to other OSes like Linux I would suggest you take a look at OpenGL as your primary graphics library.

Lesson 1 - Using DirectDraw to set the video mode

Before we begin, I assume you have a basic knowledge of Windows programming. If you don't then send me an e-mail and I will try and have a basic tutorial in the next issue of Hugi.

First, though, we need to be able to enable DirectDraw and destroy it as needed. Destroying DD is not essential but if you don't, it will clog up the PC's memory, which is never good.

To create a DD object within C++, Microsoft has supplied a useful function called DirectDrawCreate used as follows:

--- Code 1..Creating a Direct Draw object ----------------------------

LPDIRECTDRAW lpdd = NULL; // 	This is the storage for the 
			  //	Direct Draw object. Put this with your
			  //	other globals.


// Create the DD object...

DirectDrawCreate(NULL, &lpdd, NULL);

-----------------------------------------------------------------------

**Please note that this will only start the original Direct X interface, not the newer DirectX versions**

This is all well and good but it is sensible to check for errors along the way with your code so you know what's happening as it goes along.

While in the testing stage with my programs I like to create a log file which shows what is going on along the way. You should be able to do this yourself but if you're having problems then mail me and I will go through it with you.

DirectDraw has a few standard returns for all its functions. Look through the documentation that comes with DirectX's SDK for more information. But with a function like DirectDrawCreate, I just like to check for total success, which is denoted by the DD_OK return.

Here is how to check for DD_OK (total success) with DirectDrawCreate.

--- Code 2...Checking for success with DirectDrawCreate --------------------

if (DirectDrawCreate(...)!=DD_OK)
{
	// some sort of problem. Use the other error
        // macros to find out what the prob is.
}

---------------------------------------------------------------------------

The next function in the initialisation stage of our demo is to set the cooperation level. You need to tell Windows how to handle your demo. Being a multitasking OS, Windows needs to be able to manage all the programs open and can be quite controlling. This moment is the time where you can tell Windows what's what and make some demands. Take a look at the DirectX documentation for the kind of demands you can make but I will go through the four that will be most useful for demo coding. Here is the code.

--- Code 3...Making those demands -------------------------------------------

if (lpdd->SetCooperativeLevel(main_window_handle, 
				DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
				DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!=DD_OK);
{
//error
}

-----------------------------------------------------------------------------

Ok, let's go through these demands.

DDSCL_FULLSCREEN sets the window to fullscreen. This is always best because you don't have to worry what the other windows on the screen look like.

DDSCL_ALLOWMODEX allows you to access those weird but wonderful ModeX resolutions like 320x240, good for nice graphics on slow computers.

DDSCL_EXCLUSIVE requests the exclusive level, MUST be used with the DDSCL_FULLSCREEN flag.

DDSCL_ALLOWREBOOT: Be nice. Let your users reboot.

Setting the display

This is where, at last, your code will make a visible difference. Setting the display. Obviously DirectDraw will allow you any mode your graphics card supports so consult your cards manual. In new demos, users are given the option to select which display they would like.

The display is set with a handy function called SetDisplayMode...

--- Code 4...Setting the display ------------------------------------------------

if (SetDisplayMode(640,480,8)!=DD_OK)
{
 // error
}

----------------------------------------------------------------------------------

This sets the display to 640*480*8bpp. 8bpp is 256 colours, unless you are a superb graphics artist then this should be enough for the time being.

In Closing

Well, that should be enough for this issue. Just this sorted before you go on, this is a crucial stage because you need to get your compiler set up properly. Just remember to include the ddraw.lib file!

Next issue we shall be looking at the palette and putting images on your new display.

Oh, don't forget to release your DDRAW interface with the release function....

--- Code 5...Shutting down DirectDraw ---

lpdd->Release();

-----------------------------------------

Don't hesitate to e-mail me at frood@xipmail.com (or frood01@sms.genie.co.uk if you want to e-mail me a text message to my mobile phone) if you have any trouble. I will help out in any way I can.

Frood.